import { useAppStore } from "@/store/modules/app";

export function setupErrorHandler() {
  const appStore = useAppStore();

  // 全局错误处理
  window.onerror = (message, _source, _lineno, _colno, error) => {
    console.error("Global error:", error);
    appStore.setError(error || new Error(String(message)));
    return true;
  };

  // Promise 错误处理
  window.addEventListener("unhandledrejection", (event) => {
    console.error("Unhandled promise rejection:", event.reason);
    appStore.setError(event.reason);
  });
}

// 错误处理函数
export function handleError(error: Error) {
  const appStore = useAppStore();
  appStore.setError(error);
  console.error("Handled error:", error);
}
